package com.yanqu.road.server.cmd.activity.lanruosi;

import com.yanqu.road.entity.activity.lanruosi.LanRuoSiConfig;
import com.yanqu.road.entity.activity.lanruosi.LanRuoSiUserData;
import com.yanqu.road.entity.activity.tripletown.Block;
import com.yanqu.road.entity.activity.tripletown.EliminateTips;
import com.yanqu.road.pb.activity.LanRuoSiProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.lanruosi.LanRuoSiModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.lanruosi.LanRuoSiMgr;
import com.yanqu.road.server.manger.activity.tripletown.EliminateLogic;
import com.yanqu.road.server.pb.LanRuoSiPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_LANRUOSI_GET_ELIMINATE_TIPS, desc = "获取消除提示")
public class LanRuoSiGetEliminateTipsCmd extends AbstractCommand {

    private int multi;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        LanRuoSiProto.LanRuoSiGetEliminateTipsReqMsg reqMsg = LanRuoSiProto.LanRuoSiGetEliminateTipsReqMsg.parseFrom(packet.getBytes());
        multi = reqMsg.getMulti();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        LanRuoSiConfig config = LanRuoSiMgr.getConfig();
        if (config == null || !ActivityMgr.activityInTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_LANRUOSI_ACTIVITY_NO_OPEN;
            return;
        }

        // 旧配置的活动不能用16倍
        if (multi == 16 && config.getFastUnlockVipLevel() == Integer.MAX_VALUE) {
            ret = GameErrorCode.E_LANRUOSI_ACTIVITY_NO_UNLOCK_MULTI;
            return;
        }

        LanRuoSiModule module = player.getModule(LanRuoSiModule.class);
        LanRuoSiUserData userData = module.getUserData();
        if (userData == null) {
            ret = GameErrorCode.E_LANRUOSI_ACTIVITY_NO_UNLOCK;
            return;
        }

        long now = System.currentTimeMillis();
        if (now - userData.getLastTipsTime() < 2500) {
            // 防止频繁请求，客户端间隔3秒
            return;
        }
        userData.setLastTipsTime(now);

        EliminateTips tips = EliminateLogic.calEliminateTips(userData.getBlockList(multi));
        if (tips.isSuccess()) {
            LanRuoSiProto.LanRuoSiGetEliminateTipsRespMsg.Builder builder = LanRuoSiProto.LanRuoSiGetEliminateTipsRespMsg.newBuilder();
            builder.setRet(0);
            builder.setFromBlock(LanRuoSiPb.parseBlockMsg(tips.getFrom()));
            if (tips.getTo() != null) {
                builder.setToBlock(LanRuoSiPb.parseBlockMsg(tips.getTo()));
            }
            for (Block block : tips.getBlockList()) {
                builder.addBlockList(LanRuoSiPb.parseBlockMsg(block));
            }
            player.sendPacket(ClientProtocol.U_LANRUOSI_GET_ELIMINATE_TIPS, builder);
        } else {
            log.error("LanRuoSiMgr calEliminateTips false on LanRuoSiGetEliminateTipsCmd! activityId={}, userId={}, rearrange!", userData.getActivityId(), userData.getUserId());
            int[][] copyBlockList = EliminateLogic.getCopyBlockList(userData.getBlockList(multi));
            EliminateLogic.rearrange(copyBlockList);
            userData.setBlockList(copyBlockList, multi);
        }
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
